package  
{
	import org.flixel.*;
	/**
	 * ...
	 * @author morgan
	 */
	public class Dialog extends FlxSprite
	{
		[Embed (source = "../data/dialogbox.png")] private var img:Class;
		[Embed (source = "../data/04B_03__.TTF", fontFamily = "SMALL", embedAsCFF = "false")] public var fntSmall:String;
		public var text:FlxText;
		public var typeTimer:Number = 0;
		public var typeDelay:Number = 0.05;
		public var messages:Array = [];
		public var messageDone:Boolean = false;
		public function Dialog() 
		{
			super(0, 0, img);
			x = (FlxG.width - width) / 2;
			y = x;
			text = new FlxText(x, y, width, "");
			text.setFormat("SMALL", 8, 0x000000, "left");
			scrollFactor.x = scrollFactor.y = 0;
			text.scrollFactor.x = text.scrollFactor.y = 0;
			blend = "overlay";
		}
		
		public function addMessage(msg:String):void
		{
			messages.push(msg);
		}
		
		override public function update():void 
		{
			super.update();
			if (messages.length == 0) { kill(); return; }
			typeTimer -= FlxG.elapsed;
			if (typeTimer <= 0)
			{
				typeTimer = typeDelay;
				var n:int = text.text.length;
				if (messages[0].length == n)
				{
					messageDone = true;
				}
				else
				{
					text.text = messages[0].substr(0, n + 1)
				}
			}
			if (FlxG.keys.justPressed("X"))
			{
				if (messageDone)
				{
					messages.shift();
					text.text = "";
					messageDone = false;
				}
				else
				{
					text.text = messages[0];
				}
			}
		}
		
	}

}